package com.yunzhi.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.edais.util.Config;
import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * token工具类
 * Created by haoyx on 2017/2/8.
 */
public class TokenUtil {

    private static final Logger logger = Logger.getLogger(TokenUtil.class);

    public static String genToken(Map<String,Object> map){
        String token = "";
        try {
            if(!map.containsKey("userId") || null == map.get("userId")){
                return token;
            }
            SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dataFormat.parse("2019-01-01");
            token = JWT.create()
                    .withSubject(String.valueOf(map.get("userId")))
                    .withExpiresAt(date)
                    .withIssuedAt(new Date())
                    .sign(Algorithm.HMAC256(Config.TOKEN_HMAC256_KEY));
        }catch (Exception e){
            logger.error("token生成失败",e);
        }
        return token;
    }
}
